package com.pshcp.doctor.tool;

import com.framework.exception.BizException;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.framework.util.DateUtil;
import com.framework.util.SystemUtil;
import com.pshcp.doctor.ao.patientgroup.SaveDoctorPatientGroupAO;
import com.pshcp.domain.dto.DoctorPatientGroupDTO;
import com.pshcp.domain.model.DoctorPatientGroup;
import com.pshcp.domain.model.DoctorPatientGroupUser;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/11/3 23:52
 */
public class DoctorPatientGroupTool {

    public static DoctorPatientGroupDTO buildGetDoctorPatientGroupList() {
        DoctorPatientGroupDTO doctorPatientGroupDTO = new DoctorPatientGroupDTO();
        doctorPatientGroupDTO.setDoctorId(ReqComTool.getUserId());
        return doctorPatientGroupDTO;
    }

    public static void checkSaveDoctorPatientGroup(SaveDoctorPatientGroupAO saveDoctorPatientGroupAO) throws BizException {
        CommonTool.checkString(saveDoctorPatientGroupAO.getName(), "10140001");
    }

    public static DoctorPatientGroup buildSaveDoctorPatientGroup(SaveDoctorPatientGroupAO saveDoctorPatientGroupAO) {
        DoctorPatientGroup doctorPatientGroup = new DoctorPatientGroup();
        doctorPatientGroup.setDoctorId(ReqComTool.getUserId());
        doctorPatientGroup.setName(saveDoctorPatientGroupAO.getName());
        doctorPatientGroup.setLabels(saveDoctorPatientGroupAO.getLabels());
        return doctorPatientGroup;
    }

    public static DoctorPatientGroup buildSaveDoctorPatientGroup(DoctorPatientGroup doctorPatientGroup, SaveDoctorPatientGroupAO saveDoctorPatientGroupAO) {
        doctorPatientGroup.setName(saveDoctorPatientGroupAO.getName());
        doctorPatientGroup.setLabels(saveDoctorPatientGroupAO.getLabels());
        return doctorPatientGroup;
    }

    public static List<DoctorPatientGroupUser> buildSaveDoctorPatientGroupUserList(Long doctorPatientGroupId, List<DoctorPatientGroupUser> oldDoctorPatientGroupUserList, List<Long> patientIds) {
        Map<Long, DoctorPatientGroupUser> patientGroupUserMap = oldDoctorPatientGroupUserList.stream().collect(Collectors.toMap(DoctorPatientGroupUser::getPatientId, doctorPatientGroupUser -> doctorPatientGroupUser));
        List<DoctorPatientGroupUser> list = new ArrayList<>();
        DoctorPatientGroupUser doctorPatientGroupUser = null;
        List<Long> haveIds = new ArrayList<>();
        for (Long patientId : patientIds) {
            DoctorPatientGroupUser patientGroupUser = patientGroupUserMap.get(patientId);
            if (patientGroupUser != null) {
                haveIds.add(patientGroupUser.getId());
                patientGroupUser.setUpdateTime(DateUtil.currDate());
                list.add(patientGroupUser);
            } else {
                doctorPatientGroupUser = new DoctorPatientGroupUser();
                doctorPatientGroupUser.setId(SystemUtil.getId());
                doctorPatientGroupUser.setDoctorPatientGroupId(doctorPatientGroupId);
                doctorPatientGroupUser.setDoctorId(ReqComTool.getUserId());
                doctorPatientGroupUser.setPatientId(patientId);
                doctorPatientGroupUser.setStatus(0);
                doctorPatientGroupUser.setCreatedTime(DateUtil.currDate());
                list.add(doctorPatientGroupUser);
            }
        }
        for (DoctorPatientGroupUser patientGroupUser : oldDoctorPatientGroupUserList) {
            if (CollectionUtils.isEmpty(haveIds) || !haveIds.contains(patientGroupUser.getId())) {
                patientGroupUser.setStatus(1);
                patientGroupUser.setUpdateTime(DateUtil.currDate());
                list.add(patientGroupUser);
            }
        }
        return list;
    }

}
